package com.zhipu.balance.ui.vm.home.system.number

import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.TimeUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation
import com.zhipu.balance.listener.AddItemOnClickListener
import com.zhipu.balance.listener.AddOnClickListener
import com.zhipu.balance.listener.ChooseClickCallback
import com.zhipu.balance.mobile.dao.NumberDao
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.pop.AddPersonnelPopup
import com.zhipu.balance.pop.ModifyDeletePopup
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.home.system.number.adapter.NumberManagementAdapter
import com.zhipu.balance.ui.home.system.personnel.PersonnelActivity
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.mobile.event.Number
import com.zhipu.balance.pop.AddNumberPopup
import com.zhipu.balance.pop.SelectModifyDeletePopup
import com.zhipu.balance.ui.home.system.number.NumberManagementActivity

/**
 * 编号管理VM
 */
class NumberManagementViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService) , AddItemOnClickListener {
    /**
     * 适配器
     */
    var mNumberManagementAdapter = NumberManagementAdapter(this)

    init {
        NumberList()
    }



    fun NumberList(){
        var mNumberList = NumberDao.getInstance().allList
        (mNumberList as MutableList<Number>?)?.let {
            mNumberManagementAdapter.setInspectionNumber(it)
        }

    }





    fun AddPersonnel(){
        var mAddNumberPopup = AddNumberPopup(object : AddOnClickListener{
            override fun onSkipClicked(sKip: String?) {
                var mNumber = Number()
                /**
                 * 名称
                 */
                mNumber.name = sKip

                /**
                 * 创建时间
                 */
                mNumber.creationTime = TimeUtils.getNowMills()
                /**
                 * 添加
                 */
                if(mNumber.save()){
                    NumberList()
                }
            }
        })
        XPopup.Builder(ActivityUtils.getTopActivity())
            .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
            .autoOpenSoftInput(false)
            .asCustom(mAddNumberPopup)
            .show();
    }

    /**
     * 关闭
     */
    fun shutDown(){
        ActivityUtils.finishActivity(NumberManagementActivity::class.java)
    }


    /**
     * 点击
     */
    override fun onSkipClicked(position: Int, mObject: Any?) {
        var mNumber : Number = mObject as Number
        var mSelectModifyDeletePopup = SelectModifyDeletePopup(object : ChooseClickCallback{
            override fun onChooseClickCallback(mInt: Int) {
                when(mInt){
                    0 ->{
                        for (number in NumberDao.getInstance().allList){
                            if(number.numberId == mNumber.numberId){
                                /**
                                 * 选中
                                 */
                                number.select = true;
                                /**
                                 * 创建时间
                                 */
                                number.creationTime = TimeUtils.getNowMills()
                                /**
                                 * 添加
                                 */
                                number.update()
                            }else{
                                /**
                                 * 选中
                                 */
                                number.select = false;
                                /**
                                 * 创建时间
                                 */
                                number.creationTime = TimeUtils.getNowMills()
                                /**
                                 * 添加
                                 */
                                number.update()
                            }
                        }
                        NumberList()
                    }
                    1 ->{
                        var mAddNumberPopup = AddNumberPopup(object : AddOnClickListener{
                            override fun onSkipClicked(sKip: String?) {

                                /**
                                 * 名称
                                 */
                                mNumber.name = sKip

                                /**
                                 * 创建时间
                                 */
                                mNumber.creationTime = TimeUtils.getNowMills()
                                /**
                                 * 添加
                                 */
                                if(mNumber.update()){
                                    NumberList()
                                }
                            }
                        })
                        XPopup.Builder(ActivityUtils.getTopActivity())
                            .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                            .autoOpenSoftInput(false)
                            .asCustom(mAddNumberPopup)
                            .show();
                    }
                    2 ->{
                        if(mNumber.delete()){
                            NumberList()
                        }
                    }
                }
            }

        })
        XPopup.Builder(ActivityUtils.getTopActivity())
            .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
            .autoOpenSoftInput(false)
            .asCustom(mSelectModifyDeletePopup)
            .show();


    }

}